package com.qipay.admin.dataCount.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.login.AuthPassport;
import com.qipay.baas.model.Cp;
import com.qipay.baas.model.Order;
import com.qipay.cp.CpService;
import com.qipay.order.config.OrderChannelEnum;
import com.qipay.order.service.OrderService;
import com.qipay.order.vo.*;
import com.qipay.security.UserDetailsServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/data")
public class DataCountController extends BaseController{
    @Autowired
    CpService cpService;
    @Autowired
    OrderService orderService;

    @AuthPassport(key = "admin")
    @RequestMapping(value = "/cp",method = RequestMethod.GET)
    public ModelAndView findCpTj(@AuthenticationPrincipal UserDetailsServiceImpl loginUser,
                                 @RequestParam(value="from",required = false,defaultValue = "") String from,
                                 @RequestParam(value="to",required = false,defaultValue = "") String to,
                                 @RequestParam(value="cpid",required = false,defaultValue = "")Long cpid){
        CpTjVo cp=new CpTjVo();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(from)||StringUtils.isBlank(to)){
            Calendar calendar = Calendar.getInstance();
            from = format.format(calendar.getTime())+" 00:00";
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            to = format.format(calendar.getTime())+" 00:00";
            cpid=cp.getCpId();
        }
        Map<String,Object>params = new HashMap<>();
        List<CpTjVo> list = orderService.listCp0Tj(from,to,cpid);
        ModelAndView view = createModelAndView("cp-data");
        view.addObject("list",list);
        params.put("from",from);
        params.put("to",to);
        params.put("cpid",cpid==null?"":cpid);
        view.addObject("params",params);
        return view;
    }

    @AuthPassport(key = "admin")
    @RequestMapping(value = "/sp",method = RequestMethod.GET)
    public ModelAndView findSpTj(@AuthenticationPrincipal UserDetailsServiceImpl loginUser,
                                 @RequestParam(value="from",required = false,defaultValue = "") String from,
                                 @RequestParam(value="to",required = false,defaultValue = "") String to,
                                 @RequestParam(value="spid",required = false,defaultValue = "")Long spid){
        SpTjVo sp=new SpTjVo();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(from)||StringUtils.isBlank(to)){
            Calendar calendar = Calendar.getInstance();
            from = format.format(calendar.getTime())+" 00:00";
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            to = format.format(calendar.getTime())+" 00:00";
            spid=sp.getSpId();
        }
        ModelAndView view = createModelAndView("sp-data");
        Map<String,Object>params = new HashMap<>();
        List<SpTjVo>list = orderService.listSp0Tj(from,to,spid);
        params.put("from",from);
        params.put("to",to);
        params.put("spid",spid==null?"":spid);
        view.addObject("params",params);
        view.addObject("list",list);
        return view;
    }

    @AuthPassport(key = "admin")
    @RequestMapping(value = "/channel",method = RequestMethod.GET)
    public ModelAndView findChannelTj(@AuthenticationPrincipal UserDetailsServiceImpl loginUser,
                                      @RequestParam(value="from",required = false,defaultValue = "") String from,
                                      @RequestParam(value="to",required = false,defaultValue = "") String to,
                                      @RequestParam(value = "channelid", required = false, defaultValue = "0") int channelid){
        ChannelTjVo channel=new ChannelTjVo();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(from)||StringUtils.isBlank(to)){
            Calendar calendar = Calendar.getInstance();
            from = format.format(calendar.getTime())+" 00:00";
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            to = format.format(calendar.getTime())+" 00:00";
            //channelid=channel.getChannelid();
        }
        ModelAndView view = createModelAndView("channel");
        Map<String,String>params = new HashMap<>();
        List<ChannelTjVo>list = orderService.listChannel0Tj(from,to,channelid);
        params.put("from",from);
        params.put("to",to);
        params.put("channelid", String.valueOf(channelid));
        view.addObject("params",params);
        view.addObject("list",list);
        view.addObject("channels",OrderChannelEnum.map());
        return view;
    }


    @AuthPassport(key = "admin")
    @RequestMapping(value = "/sp-channel",method = RequestMethod.GET)
    public ModelAndView findSpChannelTj(@AuthenticationPrincipal UserDetailsServiceImpl loginUser,
                                        @RequestParam(value="from",required = false,defaultValue = "") String from,
                                        @RequestParam(value="to",required = false,defaultValue = "") String to,
                                        @RequestParam(value="spid",required = false) String spid,
                                        @RequestParam(value = "channelid", required = false, defaultValue = "0") int channelid){
        if("".equals(spid))spid=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(from)||StringUtils.isBlank(to)){
            Calendar calendar = Calendar.getInstance();
            from = format.format(calendar.getTime())+" 00:00";
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            to = format.format(calendar.getTime())+" 00:00";
        }
        ModelAndView view = createModelAndView("channel-sp");
        Map<String,Object>params = new HashMap<>();
        List<ChannelSpTjVo> list = orderService.listChannelSp0Tj(from,to,spid,channelid);
        params.put("from",from);
        params.put("to",to);
        params.put("spid",spid==null?"":spid);
        params.put("channelid",String.valueOf(channelid));
        view.addObject("channels",OrderChannelEnum.map());
        view.addObject("params",params);
        view.addObject("list",list);
        return view;
    }


    @AuthPassport(key = "admin")
    @RequestMapping(value = "/cp-channel",method = RequestMethod.GET)
    public ModelAndView findCpSpTj(@AuthenticationPrincipal UserDetailsServiceImpl loginUser,
                                   @RequestParam(value="from",required = false,defaultValue = "") String from,
                                   @RequestParam(value="to",required = false,defaultValue = "") String to,
                                   @RequestParam(value="cpid",required = false) String cpid,
                                   @RequestParam(value = "channelid", required = false, defaultValue = "0") int channelid){
        if("".equals(cpid))cpid=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(from)||StringUtils.isBlank(to)){
            Calendar calendar = Calendar.getInstance();
            from = format.format(calendar.getTime())+" 00:00";
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            to = format.format(calendar.getTime())+" 00:00";
        }
        ModelAndView view = createModelAndView("cp-channel-data");
        Map<String,Object> params = new HashMap<>();
        List<Cp_SpTjVo>list = orderService.listCp_Sp0Tj(from, to,cpid,channelid);
        params.put("from",from);
        params.put("to",to);
        params.put("cpid",cpid==null?"":cpid);
        params.put("channelid",String.valueOf(channelid));
        view.addObject("channels",OrderChannelEnum.map());
        view.addObject("params",params);
        view.addObject("list",list);
        return view;
    }

}
